package _2022.hot100._7_整数反转;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author： YHSimon
 * @date： 2022-03-29 14:51
 */
public class Solution {
    //-2147483648  2147483647  0ms 38.4MB
    public int reverse2(int x){
        int res=0;
        while(x!=0){
            int tmp=x%10;
            if(res>Integer.MAX_VALUE/10||(res==Integer.MIN_VALUE/10&&tmp>7)){
                return 0;
            }
            if(res<Integer.MIN_VALUE/10||(res==Integer.MIN_VALUE/10&&tmp<-8)){
                return 0;
            }
            res=res*10+tmp;
            x/=10;
        }
        return res;
    }

    //3ms 39.4MB
    public int reverse(int x) {
        String s = String.valueOf(x);
        Deque<Character> stack=new LinkedList<>();
        boolean flag=x<0;
        for(char c:s.toCharArray()){
            if(c=='-'){
                continue;
            }
            stack.push(c);
        }
        StringBuilder sb=new StringBuilder();
        while(stack.size()>0){
            sb.append(stack.pop());
        }
        long res=flag ?-Long.valueOf(sb.toString()):Long.valueOf(sb.toString());
        if(res<Integer.MIN_VALUE||res>Integer.MAX_VALUE){
            return 0;
        }
        return (int)res;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.reverse(1534236469));

        System.out.println(Integer.MAX_VALUE+" "+Integer.MIN_VALUE);
    }
}
